package modulesAPI.v2.composite;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;

import com.caterina.components.CLabel;
import com.caterina.components.CPanel;
import com.caterina.resources.CColors;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import budget.Service;
import budget.Service.ServiceType;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCheckBox;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModDoubleTextField;
import modulesAPI.v2.basics.ModTextField;

public class MService extends Module<Service>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un servizio
	 * */
	public MService(Module<?> ownerModule, Service content, String tag,String description, ViewType viewType,int...options) {
		this(ownerModule, content,-1, tag, description, viewType,options);
	}
	public MService(Module<?> ownerModule, Service content,int quantity, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		this.quantity=quantity;
		this.createModules();
		if(this.quantity>-1){
			this.addModules(name,serviceType,cost,perPerson,onlyOne,atDay,selected);
		}else
			this.addModules(name,serviceType,cost,perPerson,onlyOne,atDay);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un servizio
	 * */
	public MService(Module<?> ownerModule,String tag,String description, ViewType viewType,int...options){
		this(ownerModule, new Service(ServiceType.ARTIST, "", 0), tag, description, viewType, options);
	}
	private void createModules(){
		this.name = new ModTextField(this, this.getContent().getName(), "SERVICE_NAME", "SERVICE_NAME_DESC", this.getViewType());
		this.serviceType=new ModComboBox(this, this.getContent().getServiceType(), "SERVICE_SERVICE_TYPE", "SERVICE_SERVICE_TYPE_DESC", this.getViewType(), Service.ServiceType.values());
		this.cost=new ModDoubleTextField(this, this.getContent().getCost(), "SERVICE_COST", "SERVICE_COST_DESC", this.getViewType());
		this.perPerson=new ModCheckBox(this, this.getContent().isPerPerson(), "SERVICE_PER_PERSON", "SERVICE_PER_PERSON_DESC", this.getViewType());
		this.onlyOne=new ModCheckBox(this, this.getContent().isOnlyOne(), "SERVICE_ONLY_ONE","SERVICE_ONLY_ONE_DESC", this.getViewType());
		this.atDay=new ModCheckBox(this, this.getContent().isAtDay(), "SERVICE_AT_DAY", "SERVICE_AT_DAY_DESC", this.getViewType());
		if(this.quantity>-1){
				boolean checked=false;
				if (this.quantity>0) checked=true;
				this.selected=new ModCheckBox(this,checked, "SERVICE_SELECTED", "SERVICE_SELECTED_DESC", ViewType.WRITABLE);
		}
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	private String getMarkerUrl(String markerName,boolean isSelected){
		final String baseUrl="graphics/services/";
		if(isSelected)return baseUrl+markerName+"_dark.png";
		else return baseUrl+markerName+"_light.png";
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		CPanel retPanel=new CPanel(new BorderLayout());
		retPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND));
		//Pannello delle caratteristiche
		CPanel rightPanel=new CPanel();
		CLabel one=new CLabel("");
		one.setIcon(new ImageIcon(this.getMarkerUrl("icOne", isSelected)));
		CLabel day=new CLabel("");
		day.setIcon(new ImageIcon(this.getMarkerUrl("icDay", isSelected)));
		CLabel person=new CLabel("");
		person.setIcon(new ImageIcon(this.getMarkerUrl("icPerson", isSelected)));
		CLabel empty=new CLabel("");
		empty.setIcon(new ImageIcon("graphics/empty.png"));

		//Pannello delle quantità
		if (this.quantity>0){
			CPanel leftPanel=new CPanel();
			CLabel checked=new CLabel("");
			checked.setIcon(new ImageIcon(this.getMarkerUrl("selected", isSelected)));
			leftPanel.add(checked);
			retPanel.add(leftPanel,BorderLayout.WEST);
			if (isSelected)
				leftPanel.setBackground(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
			else leftPanel.setBackground(CColors.LIST_ENTRY_BACKGROUND);
		}
		rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
		if (this.getContent().isAtDay())rightPanel.add(day);
		if (this.getContent().isOnlyOne())rightPanel.add(one);
		if (this.getContent().isPerPerson())rightPanel.add(person);
		if (rightPanel.getComponents().length==0)rightPanel.add(empty);
		retPanel.add(rightPanel,BorderLayout.EAST);
		retPanel.add(CModules.formatForList(
				this.getContent().getName(),CWords.getWord(this.getContent().getServiceType().name())+" "+CWords.getWord("CURRENCY")+this.getContent().getCost(), "", isSelected),
				BorderLayout.CENTER);
		if (isSelected)
			rightPanel.setBackground(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
		else rightPanel.setBackground(CColors.LIST_ENTRY_BACKGROUND);
		return retPanel;
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return CModules.formatPanelForReadOnlyPairs(this.getContent().getName(), Double.toString(this.getContent().getCost()));
	}
	public void setQuantity(int quantity){
		this.quantity=quantity;
		this.fireModuleChanged(this);
	}
	public int getQuantity(){
		return this.quantity;
	}
	protected void beforeFireChange(Module<?>mod){
		if (this.quantity>-1){
			if(selected.getContent())
					this.quantity=1;
				else this.quantity=0;
		}
		this.setContent(new Service(
				this.getContent().getId(), 
				(ServiceType) serviceType.getContent(),
				name.getContent(),
				cost.getContent(),
				this.onlyOne.getContent(),
				this.perPerson.getContent(),
				this.atDay.getContent()
			)
		);
	}
	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	private ModCheckBox selected;
	private ModCheckBox perPerson;
	private ModCheckBox atDay;
	private ModCheckBox onlyOne;
	private ModComboBox serviceType;
	private ModDoubleTextField cost;
	private ModTextField name;
	int quantity;
}
